<?php
/**
* Class FkAC into Bo Controller
* @author SillySmart
* @copyright SillySmart
* @package Mvc.Controllers.Bo
* @see Mvc.Controllers.Bo.ControllerProtected
* @see Mvc.Controllers.SiteProtected
* @see Sls.Controllers.Core.SLS_GenericController
* @since 1.0
*
*/
class BoFkAC extends BoControllerProtected
{
	public function init()
	{
		parent::init();
	}

	public function action()
	{
		$xml = $this->getXML();

		// Check account rights
		$right = SLS_BoRights::isAuthorized("add",ucfirst($this->_http->getParam("Db"))."_".$this->_http->getParam("Model"));
		if ($right == -1)
			$this->_generic->redirect("Bo/Connection.sls");
		else if ($right == 0)
			$this->_generic->redirect("Bo/Connection.sls");
		// /Check account rights

		// Params
		$model 	= $this->_http->getParam("Model");
		$db 	= $this->_http->getParam("Db");
		$column = $this->_http->getParam("Column");
		$keyword= $this->_http->getParam("Keyword");
		$lang 	= $this->_http->getParam("Lang");
		$id 	= $this->_http->getParam("Id");

		// Objects
		$this->_generic->useModel(SLS_String::tableToClass($model),$db,"user");
		$class = ucfirst($db)."_".SLS_String::tableToClass($model);
		$currentModel = new $class();

		// Check really fk
		$xmlFk = new SLS_XMLToolbox(file_get_contents($this->_generic->getPathConfig("configSls")."/fks.xml"));
		$res = $xmlFk->getTagsByAttributes("//sls_configs/entry",array("tableFk","columnFk"),array($db."_".$currentModel->getTable(),$column));
		if (empty($res))
			$this->_generic->redirect("Bo/Connection.sls");

		// Objects
		$resultFk = array_shift($xmlFk->getTagsAttribute("//sls_configs/entry[@tableFk='".$db."_".$currentModel->getTable()."' and @columnFk='".$column."' and @multilanguage = 'false']","multilanguage"));
		$labelPk = substr($res,(strpos($res,'labelPk="')+9),(strpos($res,'" tablePk="')-(strpos($res,'labelPk="')+9)));
		$tableTm = substr($res,(strpos($res,'tablePk="')+9),(strpos($res,'"/>')-(strpos($res,'tablePk="')+9)));
		$tablePk = SLS_String::substrAfterFirstDelimiter($tableTm,"_");
		$dbPk 	 = SLS_String::substrBeforeFirstDelimiter($tableTm,"_");
		$this->_generic->useModel($tablePk,$dbPk,"user");
		$classFk = ucfirst($dbPk)."_".SLS_String::tableToClass($tablePk);
		$objectFk = new $classFk();
		$columns = array();
		$columnsLabel = array();
		$clause = array();
		$render = array();
		foreach($objectFk->getParams() as $key => $value)
		{
			array_push($columns,"`".$key."`");
			if (SLS_String::contains($labelPk,$key))
				$columnsLabel[$key] = strpos($labelPk,$key);
		}
		array_multisort($columnsLabel);
		foreach($columnsLabel as $columnLabel => $offset)
			array_push($clause,$columnLabel);
		$pattern = str_replace("'","''",$labelPk);
		foreach($clause as $columnC)
			$pattern = str_replace($columnC,"',".$columnC.",'",$pattern);

		$results = $currentModel->fkAC($currentModel->getTable(),$objectFk->getTable(),$objectFk->getPrimaryKey(),$columns,"CONCAT('".$pattern."')",($objectFk->isMultilanguage()) ? $lang : "",implode('%',explode(' ',trim($keyword))),10,$id);
		for($i=0 ; $i<$count=count($results) ; $i++)
		{
			$mask = $labelPk;
			$id = 0;
			$label = "";
			foreach($results[$i] as $key => $value)
			{
				if ($key == $objectFk->getPrimaryKey())
					$id = $value;
				if (SLS_String::contains($mask,$key))
					$mask = str_replace($key,$value,$mask);
			}
			array_push($render,array("id"=>$id,"label"=>$mask));
		}

		echo json_encode($render);
		die();
	}
}
?>